﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Forget.HRBLL.Model;
using Whidsoft.WebControls;
using Forget.HRBLL.Logical;

namespace HRWebSite.Sys
{
    public partial class DepartmentReport : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            this.HRLimits = Limits.EmployeeManage;
            this.HRLimitAttribute = Forget.HRBLL.LimitAttribute.Read;
            base.Page_Load(sender, e);
            if (!this.IsPostBack)
                BoundOrgChart();
        }

        private void BoundOrgChart()
        {
            List<Department> departs = DepartmentLogical.GetAllDepartment();
            List<OrgNode> orgNodes = new List<OrgNode>();
            foreach (var dp in departs)
            {
                OrgNode orgNode = new OrgNode();
                orgNode.ID = dp.DepartmentID.ToString();
                orgNode.Text = dp.Name;
                orgNode.Description = dp.ParentID.ToString();
                orgNode.NavigateUrl = string.Format("javascript:void({0});", dp.DepartmentID);
                orgNodes.Add(orgNode);
            }

            foreach (var org in orgNodes)
            {
                if (org.Description != "0")
                {
                    OrgNode tmp = orgNodes.Find(x => x.ID == org.Description);
                    tmp.Nodes.Add(org);
                }
            }
            this.OrgChart1.Node = orgNodes.Find(x => x.Description == "0");
        }
    }
}